////////////////////////////////////////////////
// File: GoodsController.java
// Description: Enter summary here after generation.
// Date: 2016-09-23 09:09:34
// Web: http://www.elanzhou.com/
////////////////////////////////////////////////
package com.mvc.controller;
import java.io.IOException;
import java.util.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mvc.entity.Goods;
import com.mvc.entity.Meminfo;
import com.mvc.service.DictService;
import com.mvc.service.GoodsService;
import com.mvc.service.SyscateService;
import com.pub.MyJson;
import com.pub.Page;

@Controller
@RequestMapping("GoodsXq")
public class GoodsXqController {
	protected final transient Log logger = LogFactory.getLog(GoodsXqController.class);
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private SyscateService syscateService;//品类service
	@Autowired
	private DictService dictService;

	public GoodsXqController() {
	}

	/*未审核列表*/
	@RequestMapping("Wsh.do")
	public String Wsh(HttpSession session,HttpServletRequest request,ModelMap modelMap, String pageNo, String Ajax) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(15);
		page.setVal(",'Goodtype':'求购'");
		String hql = "select a from Goods a  where a.Goodstype='求购' and a.Status='未审核' order by a.ID desc";
		List list = goodsService.getGoodsList(hql, page);
		String counthql = "select count(*) from Goods a where a.Goodstype='求购' and a.Status='未审核'";
		page.setPageCount(goodsService.getCount(counthql, page));
		modelMap.put("page", page);
		modelMap.put("list", list);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/GoodsXq/List";
		} else {
			return "/GoodsXq/Wsh";
		}

	}
	
	/*列表*/
	@RequestMapping("Index.do")
	public String Index(HttpSession session,HttpServletRequest request,ModelMap modelMap, String pageNo, String Ajax) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(15);
		page.setVal(",'Goodtype':'求购'");
		String hql = "select a from Goods a  where a.Goodstype='求购' and a.Status='已审核' order by a.ID desc";
		List list = goodsService.getGoodsList(hql, page);
		String counthql = "select count(*) from Goods a where a.Goodstype='求购' and a.Status='已审核'";
		page.setPageCount(goodsService.getCount(counthql, page));
		modelMap.put("page", page);
		modelMap.put("list", list);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/GoodsXq/List";
		} else {
			return "/GoodsXq/Index";
		}

	}
	/*添加*/ 
	@RequestMapping("Add.do")
	public String Add(HttpSession session,ModelMap modelMap)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		List<Object> fcate=syscateService.getFcate() ;
		modelMap.put("fcate", fcate);
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		String dqsj=format.format(date);
		modelMap.put("dqsj", dqsj);
		List hylist=dictService.getAllByKeyname("规格");		
		modelMap.put("hylist",hylist);
		return "/GoodsXq/Add";
	}
	/*保存*/
	@RequestMapping("Save.do")
	public void Save(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, HttpSession session) {

		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		try {
			
			Goods  goods= new Goods();
			boolean tag=true;
		    String Msg="信息保存成功";
             
               if(tag){
                              
               String FieldFname=(String)request.getParameter("Fname");
             
               if(((FieldFname!=null)&(!FieldFname.trim().equals("")))){
            	   FieldFname=FieldFname.trim();
                    goods.setFname(FieldFname);
                  
        		}else
        		{
        			tag=false;
        			Msg="品类不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldCatename=(String)request.getParameter("Catename");
             
               if(((FieldCatename!=null)&(!FieldCatename.trim().equals("")))){
            	   FieldCatename=FieldCatename.trim();
                    goods.setCatename(FieldCatename);
                  
        		}else
        		{
        			tag=false;
        			Msg="品种不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldSpec=(String)request.getParameter("Spec");
             
               if(((FieldSpec!=null)&(!FieldSpec.trim().equals("")))){
            	   FieldSpec=FieldSpec.trim();
                    goods.setSpec(FieldSpec);
                  
        		}else
        		{
        			tag=false;
        			Msg="规格不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldGoodstype=(String)request.getParameter("Goodstype");
             
               if(((FieldGoodstype!=null)&(!FieldGoodstype.trim().equals("")))){
            	   FieldGoodstype=FieldGoodstype.trim();
                    goods.setGoodstype(FieldGoodstype);
                  
        		}else
        		{
        			tag=false;
        			Msg="类型不能为空！";
        		}
        		}
               if(tag){
                   
                   String FieldGoodspic=(String)request.getParameter("Goodspic");
                 
                   if(((FieldGoodspic!=null)&(!FieldGoodspic.trim().equals("")))){
                   	FieldGoodspic=FieldGoodspic.trim();
                        goods.setGoodspic(FieldGoodspic);
                      
            		}else
            		{
            			 FieldGoodspic="/upfile/goods.jpg";
   	                 goods.setGoodspic(FieldGoodspic);
            			/*tag=false;
            			Msg="图片不能为空！";*/
            		}
            		}
             
               if(tag){
                              
               String FieldAddress=(String)request.getParameter("Address");
             
               if(((FieldAddress!=null)&(!FieldAddress.trim().equals("")))){
            	   FieldAddress=FieldAddress.trim();
                    goods.setAddress(FieldAddress);
                  
        		}else
        		{
        			tag=false;
        			Msg="产地不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldGoodsnum=(String)request.getParameter("Goodsnum");
             
               if(((FieldGoodsnum!=null)&(!FieldGoodsnum.trim().equals("")))){
            	   FieldGoodsnum=FieldGoodsnum.trim();
                    goods.setGoodsnum(FieldGoodsnum);
                  
        		}else
        		{
        			tag=false;
        			Msg="数量不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldGoodsdate=(String)request.getParameter("Goodsdate");
             
               if(((FieldGoodsdate!=null)&(!FieldGoodsdate.trim().equals("")))){
            	   FieldGoodsdate=FieldGoodsdate.trim();
                    goods.setGoodsdate(FieldGoodsdate);
                  
        		}else
        		{
        			tag=false;
        			Msg="发布时间不能为空！";
        		}
        		}
	         
             
               if(tag){
                              
               String FieldMembername=(String)request.getParameter("Membername");
             
               if(((FieldMembername!=null)&(!FieldMembername.trim().equals("")))){
            	   FieldMembername=FieldMembername.trim();
                    goods.setMembername(FieldMembername);
                  
        		}else
        		{
        			tag=false;
        			Msg="会员名不能为空！";
        		}
        		}
	         
                goods.setStatus("未审核");
            
            MyJson mj=new MyJson();
              if(tag){
                     goodsService.Save(goods);	 
        			 mj.setStatusCode(200);
        		 }else
        		 {
        			 mj.setStatusCode(300);
        		 }
             mj.setMessage(Msg);
             mj.setForwardUrl("/GoodsXq/Wsh.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
			 response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	} 
    
	/*编辑*/
	@RequestMapping("Edit.do")
	public String Edit(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		String ID=request.getParameter("ID");
		Goods goods=(Goods)goodsService.getGoods(ID);
		modelMap.put("goods", goods);
		//加载品类列表
		List<Object> fcate=syscateService.getFcate() ;
		modelMap.put("fcate", fcate);
		List hylist=dictService.getAllByKeyname("规格");		
		modelMap.put("hylist",hylist);
		return "/GoodsXq/Edit";
	}
    
    /*编辑保存*/
	@RequestMapping("EditSave.do")
	public void EditSave(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap,HttpSession session) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		try {
			String Id=(String)request.getParameter("ID");
			Goods  goods=(Goods)goodsService.getGoods(Id);
			boolean tag=true;
		    String Msg="信息修改成功";
             
            if(tag){
               
               String FieldFname=(String)request.getParameter("Fname");
             
               if(((FieldFname!=null)&(!FieldFname.trim().equals("")))){
            	   FieldFname=FieldFname.trim();
                    goods.setFname(FieldFname);
                  
        		}else
        		{
        			tag=false;
        			Msg="品类不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldCatename=(String)request.getParameter("Catename");
             
               if(((FieldCatename!=null)&(!FieldCatename.trim().equals("")))){
            	   FieldCatename=FieldCatename.trim();
                    goods.setCatename(FieldCatename);
                  
        		}else
        		{
        			tag=false;
        			Msg="品种不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldSpec=(String)request.getParameter("Spec");
             
               if(((FieldSpec!=null)&(!FieldSpec.trim().equals("")))){
            	   FieldSpec=FieldSpec.trim();
                    goods.setSpec(FieldSpec);
                  
        		}else
        		{
        			tag=false;
        			Msg="规格不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldGoodstype=(String)request.getParameter("Goodstype");
             
               if(((FieldGoodstype!=null)&(!FieldGoodstype.trim().equals("")))){
            	   FieldGoodstype=FieldGoodstype.trim();
                    goods.setGoodstype(FieldGoodstype);
                  
        		}else
        		{
        			tag=false;
        			Msg="类型不能为空！";
        		}
        		}
	         
            if(tag){
                
                String FieldGoodspic=(String)request.getParameter("Goodspic");
              
                if(((FieldGoodspic!=null)&(!FieldGoodspic.trim().equals("")))){
             	   FieldGoodspic=FieldGoodspic.trim();
                     goods.setGoodspic(FieldGoodspic);
                   
         		}else
         		{
		    			 FieldGoodspic="/upfile/goods.jpg";
		                 goods.setGoodspic(FieldGoodspic);
         			/*tag=false;
         			Msg="图片不能为空！";*/
         		}
         		}
            if(tag){
               
               String FieldAddress=(String)request.getParameter("Address");
             
               if(((FieldAddress!=null)&(!FieldAddress.trim().equals("")))){
            	   FieldAddress=FieldAddress.trim();
                    goods.setAddress(FieldAddress);
                  
        		}else
        		{
        			tag=false;
        			Msg="产地不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldGoodsnum=(String)request.getParameter("Goodsnum");
             
               if(((FieldGoodsnum!=null)&(!FieldGoodsnum.trim().equals("")))){
            	   FieldGoodsnum=FieldGoodsnum.trim();
                    goods.setGoodsnum(FieldGoodsnum);
                  
        		}else
        		{
        			tag=false;
        			Msg="数量不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldGoodsdate=(String)request.getParameter("Goodsdate");
             
               if(((FieldGoodsdate!=null)&(!FieldGoodsdate.trim().equals("")))){
            	   FieldGoodsdate=FieldGoodsdate.trim();
                    goods.setGoodsdate(FieldGoodsdate);
                  
        		}else
        		{
        			tag=false;
        			Msg="发布时间不能为空！";
        		}
        		}
	         
             
            if(tag){
               
               String FieldMembername=(String)request.getParameter("Membername");
             
               if(((FieldMembername!=null)&(!FieldMembername.trim().equals("")))){
            	   FieldMembername=FieldMembername.trim();
                    goods.setMembername(smeminfo.getMemname());
                  
        		}else
        		{
        			tag=false;
        			Msg="会员名不能为空！";
        		}
        		}
	         
            if(tag){
                
                String FieldStatus=(String)request.getParameter("Status");
              
                if(((FieldStatus!=null)&(!FieldStatus.trim().equals("")))){
                	FieldStatus=FieldStatus.trim();
                     goods.setMembername(smeminfo.getMemname());
                   
         		}else
         		{
         			tag=false;
         			Msg="状态不能为空！";
         		}
         		}
 	         
            
            MyJson mj=new MyJson();
              if(tag){
                     goodsService.EditSave(goods);	 
        			 mj.setStatusCode(200);
        		 }else
        		 {
        			 mj.setStatusCode(300);
        		 }
             mj.setMessage(Msg);
             mj.setForwardUrl("/GoodsXq/Wsh.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
			 response.getWriter().print(str);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	} 
    
	/*删除*/
	@RequestMapping("Del.do")
	public void Del(HttpSession session,HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
             String ID=request.getParameter("ID");
             goodsService.Del(ID);
             String Msg="数据删除成功";
             MyJson mj=new MyJson();
             mj.setStatusCode(200);
             mj.setMessage(Msg);
             mj.setForwardUrl("/GoodsXq/Wsh.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
             try {
                  response.getWriter().print(str);
                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }
	      }
	
	@RequestMapping("Sh.do")
	public void Sh(HttpSession session,HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
             String ID=request.getParameter("ID");
             Goods good=goodsService.getGoods(ID);
             good.setStatus("已审核");
             goodsService.EditSave(good);
             String Msg="审核成功";
             MyJson mj=new MyJson();
             mj.setStatusCode(200);
             mj.setMessage(Msg);
             mj.setForwardUrl("/GoodsXq/Wsh.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
             try {
                  response.getWriter().print(str);
                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }
	      }
	@RequestMapping("Qxsh.do")
	public void Qxsh(HttpSession session,HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
             String ID=request.getParameter("ID");
             Goods good=goodsService.getGoods(ID);
             good.setStatus("未审核");
             goodsService.EditSave(good);
             String Msg="取消成功";
             MyJson mj=new MyJson();
             mj.setStatusCode(200);
             mj.setMessage(Msg);
             mj.setForwardUrl("/GoodsXq/Index.do");
             mj.setCallbackType("forward");
             String str=mj.getJson();
             try {
                  response.getWriter().print(str);
                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }
	      }
	
          	/*编辑*/
	@RequestMapping("Detail.do")
	public String Detail(HttpSession session,HttpServletRequest request,ModelMap modelMap)
	{
		Meminfo smeminfo=(Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo",smeminfo);
		
		String ID=request.getParameter("ID");
		Goods goods=(Goods)goodsService.getGoods(ID);
		modelMap.put("goods", goods);
		return "/GoodsXq/Detail";
	}
    
 
}
